package cn.gogoyc.user.controller;

import cn.gogoyc.org.basic.dto.EmailCodeDto;
import cn.gogoyc.org.basic.exception.BusinessException;
import cn.gogoyc.org.basic.util.AjaxResult;
import cn.gogoyc.org.basic.util.PageList;
import cn.gogoyc.user.domain.User;
import cn.gogoyc.user.query.UserQuery;
import cn.gogoyc.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 前台用户后端接口类
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * The User service.
     */
    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     *
     * @param user 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user) {
        try {
            if (user.getId() != null) {
                userService.update(user);
            } else {
                userService.save(user);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败", false);
        }
    }

    /**
     * 接口：删除
     *
     * @param id the id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            userService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败", false);
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids the ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            userService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("批量删除失败", false);
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id the id
     * @return the user
     */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id") Long id) {
        return userService.loadById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return list
     */
    @GetMapping
    public List<User> findAll() {
        return userService.loadAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }

    /**
     * Register phone ajax result.
     *
     * @param map the map
     * @return the ajax result
     */
    @PostMapping("/register/phone")
    public AjaxResult registerPhone(@RequestBody Map<String, String> map) {
        try {
            userService.registerPhone(map);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络繁忙..请稍后再试", false);
        }
    }

    /**
     * Audit email ajax result.
     *
     * @param emailCodeDto the email code dto
     * @return the ajax result
     */
    @PostMapping("/register/email")
    public AjaxResult auditEmail(@RequestBody EmailCodeDto emailCodeDto) {
        try {
            userService.auditEmail(emailCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络繁忙..请稍后再试..", false);
        }
    }

    /**
     * Activated email string.
     *
     * @param email    the email
     * @param pwd      the pwd
     * @param response the response
     * @return the string
     */
    @GetMapping("/active/{email}/{pwd}")
    public String activatedEmail(@PathVariable("email")String email,
                                 @PathVariable("pwd")String pwd,
                                 HttpServletResponse response) {
        try {
            userService.activatedEmail(email, pwd);
            response.sendRedirect("http://127.0.0.1/login.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
